package com.driver.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.driver.enums.ResponseCode;
import com.driver.pojo.entity.Temperature;
import com.driver.pojo.query.TemperatureQueryPage;
import com.driver.pojo.response.R;
import com.driver.service.ITemperatureService;
import com.driver.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/temperature")
@RequiredArgsConstructor
@Slf4j
public class TemperatureController {

    private final ITemperatureService temperatureService;

    @PostMapping("/list")
    public R<List<Temperature>> list(@RequestBody Temperature temperature) {
        LambdaQueryWrapper<Temperature> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(temperature.getCode()), Temperature::getCode, temperature.getCode())
                .eq(Objects.nonNull(temperature.getIsValid()), Temperature::getIsValid, temperature.getIsValid())
                .orderByAsc(Temperature::getCreateTime);
        return R.success(temperatureService.list(wrapper));
    }

    @PostMapping("/page")
    public R<Page<Temperature>> page(@RequestBody TemperatureQueryPage query) {
        Page<Temperature> page = new Page<>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<Temperature> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Temperature::getCreateTime);
        page = temperatureService.page(page, wrapper);
        return R.success(page);
    }

    @PostMapping({"/add", "/update"})
    public R<Void> saveOrUpdate(@RequestBody Temperature temperature) {
        if (Objects.isNull(temperature.getId())) {
            // 获取用户信息
        } else {
            // 获取用户信息
        }
        boolean b = temperatureService.saveOrUpdate(temperature);
        if (b) {
            return R.success();
        } else {
            return R.fail(ResponseCode.ERROR_DATABASE);
        }
    }

    @GetMapping("/exportPdf")
    public void exportPdf(TemperatureQueryPage query, HttpServletResponse response) {
        Page<Temperature> page = new Page<>(query.getPageNo(), query.getPageSize());
        LambdaQueryWrapper<Temperature> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Temperature::getCreateTime);
        page = temperatureService.page(page, wrapper);

        // 假设存在一个TemperatureService的方法generatePdf来生成PDF文件
        byte[] pdfBytes = temperatureService.generatePdf(page.getRecords());

        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=temperature_data.pdf");
        try {
            response.getOutputStream().write(pdfBytes);
        } catch (IOException e) {
            log.error("Error while exporting PDF", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    }

}
